using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Helpers;
	using System.Web.Security;
    using TemplateWeb.DTO;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectNguoiChoiDauGia()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectNguoiChoiDauGiaByIDNguoiDung(Guid idnguoidung)
		{
			var data = new DataTransferViewModel
			{
				IDNguoiDung = idnguoidung,
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
			};
			return View("SelectNguoiChoiDauGia", CreateViewModel(data));
		}

		public ActionResult SelectNguoiChoiDauGiaByIDNguoiDungPaging(Guid idnguoidung)
		{
			var data = new DataTransferViewModel
			{
				IDNguoiDung = idnguoidung,
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
			};
			return PartialView("Templates/TH_ListNguoiChoiDauGia", CreateViewModel(data));
		}

		public ActionResult SelectNguoiChoiDauGiaByIDPhienDauGia(int idphiendaugia)
		{
			var data = new DataTransferViewModel
			{
				IDPhienDauGia = idphiendaugia,
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
			};
			return View("SelectNguoiChoiDauGia", CreateViewModel(data));
		}

		public ActionResult SelectNguoiChoiDauGiaByIDPhienDauGiaPaging(int idphiendaugia)
		{
			var data = new DataTransferViewModel
			{
				IDPhienDauGia = idphiendaugia,
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
			};
			return PartialView("Templates/TH_ListNguoiChoiDauGia", CreateViewModel(data));
		}


		public ActionResult DeleteNguoiChoiDauGia(int id, int page = 1)
		{
			var result = _repNguoiChoiDauGia.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
				CurrentPage = page
			};
			return View("SelectNguoiChoiDauGia", CreateViewModel(data));
		}

		public ActionResult EditNguoiChoiDauGia(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditNguoiChoiDauGia", CreateViewModel(data));
		}

		public ActionResult CancelEditingNguoiChoiDauGia(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListNguoiChoiDauGia", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateNguoiChoiDauGia(FormCollection forms)
		{
			string id = forms.GetValues("NguoiChoiDauGia_ID")[0];
			string idnguoidung = forms.GetValues("NguoiChoiDauGia_IDNguoiDung")[0];
			string idphiendaugia = forms.GetValues("NguoiChoiDauGia_IDPhienDauGia")[0];
			string thoidiemdaugia = forms.GetValues("NguoiChoiDauGia_ThoiDiemDauGia")[0];
			string giaduara = forms.GetValues("NguoiChoiDauGia_GiaDuaRa")[0];
			var nguoichoidaugia = _repNguoiChoiDauGia.SelectByID(int.Parse(id));
			nguoichoidaugia.IDNguoiDung = Guid.Parse(idnguoidung.Replace(".", "").Replace(",", ""));
			nguoichoidaugia.IDPhienDauGia = int.Parse(idphiendaugia.Replace(".", "").Replace(",", ""));
			nguoichoidaugia.ThoiDiemDauGia = DateTime.Now;
			nguoichoidaugia.GiaDuaRa = long.Parse(giaduara.Replace(".", "").Replace(",", ""));
			var result = _repNguoiChoiDauGia.Update(nguoichoidaugia);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
			};
			return View("SelectNguoiChoiDauGia", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertNguoiChoiDauGia(FormCollection forms)
		{
			string idnguoidung = forms.GetValues("NguoiChoiDauGia_IDNguoiDung")[0];
			string idphiendaugia = forms.GetValues("NguoiChoiDauGia_IDPhienDauGia")[0];
			string thoidiemdaugia = forms.GetValues("NguoiChoiDauGia_ThoiDiemDauGia")[0];
			string giaduara = forms.GetValues("NguoiChoiDauGia_GiaDuaRa")[0];
			var newItem = new NguoiChoiDauGiaDTO
			{
				IDNguoiDung = Guid.Parse(idnguoidung),
				IDPhienDauGia = int.Parse(idphiendaugia),
				ThoiDiemDauGia = thoidiemdaugia == "" ? DateTime.Now : DateTime.Parse(thoidiemdaugia),
				GiaDuaRa = long.Parse(giaduara)
			};
			var result = _repNguoiChoiDauGia.Insert(newItem);
			var data = new DataTransferViewModel
			{
				IDPhienDauGia = int.Parse(idphiendaugia),
				EnumViewModelType = EnumViewModel.ADMIN_NGUOICHOIDAUGIA,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectNguoiChoiDauGia", CreateViewModel(data));
		}


	}
}